<?php
function dpa_detailview( $user_id, $achievement ) {
	$highlight = '';
	$earnt = dpa_get_ea_details( $user_id, $achievement['short_name'] );

	if ( !empty( $earnt ) ) {
		$highlight = 'highlight';
	}
?>
	<div class="detailview">

		<img src="http://www.gravatar.com/avatar/<?php echo md5( $achievement['short_name'] ); ?>?s=50&amp;d=wavatar" alt="" width="50" height="50" />
		<div><span class="dpa-title"><?php echo apply_filters( 'dpa_filter_achievement_text', $achievement['nice_name'] ); ?></span> - <?php echo apply_filters( 'dpa_filter_achievement_text', $achievement['points'] ) . __(' points', 'dp-achievements'); ?></div>
		<div><?php echo apply_filters( 'dpa_filter_achievement_text', $achievement['description'] ); ?></div>

		<?php if ( !empty( $earnt ) ) { ?>
			<div class="<?php echo $highlight; ?>"><?php echo apply_filters( 'dpa_filter_achievement_text', mysql2date( get_option( 'date_format' ), $earnt['achieved_at'] ) ); ?></div>
		<?php } ?>
	</div>
<?php
}

function dpa_draw_categorycompletiondetails( $user_id ) {
	//number of ACTIVE achievements PER CATEGORY
	$categories = dpa_get( 'DISTINCT category', array( array( 'type' => 'd', 'key' => 'active', 'value' => 1 ) ) );
	$total_achievements = array();
	$earnt_achievements = array();

	foreach ( $categories as $category ) {
		$total_achievements[$category] = dpa_get( 'COUNT(id)', array( array( 'type' => 's', 'key' => 'category', 'value' => $category ) ) );
	}

	//number of EARNT achievements PER CATEGORY
	foreach ( $categories as $category ) {
		$earnt_achievements[$category] = dpa_get_ea( 'COUNT(short_name)', array( array( 'type' => 'd', 'key' => 'user_id', 'value' => $user_id ), array( 'type' => 's', 'key' => 'short_name', 'value' => '' ), array( 'type' => 's', 'key' => 'category', 'value' => $category ) ) );
	}

	$stats = array();
	foreach ( $categories as $category ) {
		$stats[$category] = array( 'name' => $category,
															 'total' => $total_achievements[$category],
															 'earnt' => $earnt_achievements[$category],
															 'percentage' => ( $earnt_achievements[$category] / $total_achievements[$category] ) * 100 );
	}
	
	foreach ( $stats as $s ) {
	?>
	<div class="graph thirdsize">
		<div class="bar" style="width: <?php echo $s['percentage']; ?>%"></div>
		<div class="caption"><p><?php echo $s['name']; ?>: <?php echo $s['earnt']; ?> / <?php echo $s['total']; ?></p></div>
	</div>
	<?php
	}
}

function dpa_draw_overallcompletiondetails( $user_id ) {
	$earnt  = (int) dpa_count_of_ea( $user_id );
	$active = (int) dpa_get( 'COUNT(id)', array( array( 'type' => 'd', 'key' => 'active', 'value' => 1 ) ) );
	$percentage = ($earnt / $active) * 100;
	?>
	<div class="graph fullsize">
		<div class="bar" style="width: <?php echo $percentage; ?>%"></div>
		<div class="caption"><p>Total Completed: <?php echo $earnt; ?> / <?php echo $active; ?></p></div>
	</div>
	<?php
}
?>